package com.zxl.register.base.business;

import com.zxl.register.base.Event;
import com.zxl.register.base.ServiceInstance;
import com.zxl.register.server.event.EventPool;
import com.zxl.register.util.JsonUtil;

import java.io.IOException;
import java.nio.channels.AsynchronousSocketChannel;

/**
 *  解析 client传送过来的消息
 */
public class AnalyticalMsg implements BusinessFun {
    @Override
    public void doSomeThing(AsynchronousSocketChannel socketChannel,String content) {
        String addr= null;
        try {
            addr = socketChannel.getRemoteAddress().toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Event event = null;
        try {
            event = JsonUtil.gson.fromJson(content, Event.class);
        } catch (Exception e) {
            System.out.println(" process read msg happen exception ! socket : " + addr);
            return;
        }
        if(event==null){
            return;
        }
        ServiceInstance serviceInstance = event.getInstance();
        if (serviceInstance == null) {
            serviceInstance = new ServiceInstance();
        }
        serviceInstance.setAddress(addr);
        serviceInstance.setLastHeartBeat(System.currentTimeMillis());
        serviceInstance.setSocketChannel(socketChannel);
        event.setReceivedTime(System.currentTimeMillis());
        EventPool.push(event);
    }
}
